package com.kstudio.ksnapshot.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

import com.kstudio.ksnapshot.KUtil;

public class KSubIconView extends View {
    private KUtil log = new KUtil("KSubIconView");

    public static final int SubIconNone = 0;

    private Context context;

    private Bitmap IconMain;
    private Rect IconMainBound;
    private int IconMainWidth;
    private int IconMainHegiht;
    private int IconMainFrameWidth;
    private int IconMainFrameHegiht;

    private Bitmap IconSub;
    private Rect IconSubBound;
    private int IconSubWidth;
    private int IconSubHegiht;

    private Paint IconPaint = new Paint();
    private boolean LayoutChanged;

    public KSubIconView(Context context) {
        this(context, null);
    }

    public KSubIconView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public KSubIconView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        this.context = context;
        init(attrs, defStyle);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = IconMainFrameWidth + IconSubWidth /4;
        width = getMeasureSize(widthMeasureSpec, width);
        int height = IconMainFrameHegiht + IconSubHegiht / 4;
        height = getMeasureSize(heightMeasureSpec, height);

        setMeasuredDimension(width, height);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right,
            int bottom) {
        if(changed || LayoutChanged) {
            if(IconMain != null) {
                int dstLeft = (IconMainFrameWidth - IconMainWidth) / 2;
                int dstRight = (IconMainFrameHegiht - IconMainHegiht) / 2;
                IconMainBound.set(dstLeft, dstRight, dstLeft + IconMainWidth, dstRight + IconMainHegiht);
            }

            if(IconSub != null) {
                int dstLeft = right - left - IconSubWidth;
                int dstRight = bottom - top - IconSubHegiht;
                IconSubBound.set(dstLeft, dstRight, right - left, bottom - top);
            }
            LayoutChanged = false;
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if(IconMain != null) {
            canvas.drawBitmap(IconMain, null, IconMainBound, IconPaint);
        }

        if(IconSub != null) {
            canvas.drawBitmap(IconSub, null, IconSubBound, IconPaint);
        }
    }

    public void setSubIconId(int id) {
        if(IconSub != null) {
            IconSub.recycle();
            IconSub = null;
            IconSubWidth = 0;
            IconSubHegiht = 0;
        }

        if(id == SubIconNone) {
            return;
        } else {
            final Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), id);
            if (bitmap == null) {
                log.e("<bitmap> requires a valid src_sub attribute");
            } else {
                IconSub = bitmap;
                IconSubWidth = IconSub.getWidth();
                IconSubHegiht = IconSub.getHeight();
                LayoutChanged = true;
                requestLayout();
            }
        }
    }

    public void setMainIconId(int id) {
        if(IconMain != null) {
            IconMain.recycle();
            IconMain = null;
            IconMainWidth = 0;
            IconMainHegiht = 0;
            IconMainFrameWidth = 0;
            IconMainFrameHegiht = 0;
        }

        final Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), id);
        if (bitmap == null) {
            log.e("<bitmap> requires a valid src_sub attribute");
        } else {
            IconMain = bitmap;
            IconMainWidth = IconMainFrameWidth = IconMain.getWidth();
            IconMainHegiht = IconMainFrameHegiht = IconMain.getHeight();
            LayoutChanged = true;
            requestLayout();
        }
    }

    public void setMainIconBMP(Bitmap b) {
        if(IconMain != null) {
            IconMain.recycle();
            IconMain = null;
            IconMainWidth = 0;
            IconMainHegiht = 0;
        }

        if (b == null) {
            log.e("<bitmap> requires a valid src_sub attribute");
        } else {
            if(b.getWidth() >= b.getHeight()) {
                IconMainWidth = IconMainFrameWidth;
                IconMainHegiht = IconMainFrameHegiht * b.getHeight() / b.getWidth();
            } else {
                IconMainWidth = IconMainFrameWidth * b.getWidth() / b.getHeight();
                IconMainHegiht = IconMainFrameHegiht;
            }

            IconMain = Bitmap.createBitmap(b);
            LayoutChanged = true;
            requestLayout();
        }
    }

    private void init(AttributeSet attrs, int defStyle) {
        IconMain = null;
        IconMainBound = new Rect();
        IconMainWidth = IconMainFrameWidth = 0;
        IconMainHegiht = IconMainFrameHegiht = 0;

        IconSub = null;
        IconSubBound = new Rect();
        IconSubWidth = 0;
        IconSubHegiht = 0;

        IconPaint = new Paint();
        LayoutChanged = true;

        int[] ids = new int[attrs.getAttributeCount()];
        for(int i = 0; i < attrs.getAttributeCount(); i++) {
            ids[i] = attrs.getAttributeNameResource(i);
        }

        TypedArray a = context.obtainStyledAttributes(attrs, ids, defStyle, 0);

        for(int i = 0; i < attrs.getAttributeCount(); i++) {
            String attrName = attrs.getAttributeName(i);
            if (attrName == null)
                continue;

            if(attrName.equals("src_main")) {
                int id = a.getResourceId(i, 0);
                final Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), id);
                if (bitmap == null) {
                    log.e("<bitmap> requires a valid src_main attribute");
                } else {
                    IconMain = bitmap;
                    IconMainWidth = IconMain.getWidth();
                    IconMainHegiht = IconMain.getHeight();
                }
            } else if(attrName.equals("src_sub")) {
                int id = a.getResourceId(i, 0);
                final Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), id);
                log.e("src_sub "+bitmap);
                if (bitmap == null) {
                    log.e("<bitmap> requires a valid src_sub attribute");
                } else {
                    IconSub = bitmap;
                    IconSubWidth = IconMainFrameWidth = IconSub.getWidth();
                    IconSubHegiht = IconMainFrameHegiht = IconSub.getHeight();
                }
            }
        }

        a.recycle();
    }

    private int getMeasureSize(int measureSpec, int cSize) {
        int measureMode = MeasureSpec.getMode(measureSpec);
        int measureSize = MeasureSpec.getSize(measureSpec);
        int size = cSize;

        if(measureMode == MeasureSpec.EXACTLY){
            size = measureSize;
        } else if(measureMode == MeasureSpec.UNSPECIFIED || measureMode == MeasureSpec.AT_MOST){
            // Check against our minimum width
            size = Math.max(size, getSuggestedMinimumWidth());

            if (measureMode == MeasureSpec.AT_MOST) {
                size = Math.min(measureSize, size);
            }
        }

        return size;
    }
}
